package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.app.service.GoodsHistoryService;
import com.junzhiit.nengji.app.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.app.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.app.web.util.ViewObjectUtil;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.GoodsAnalysisInfoVO;
import com.junzhiit.nengji.app.web.vo.GoodsAnalysisVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.bo.GoodsAnalysisBO;
import com.junzhiit.nengji.bo.GoodsAnalysisInfoBO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.GoodsHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class GoodsHistoryServiceImpl extends BaseServiceImpl implements GoodsHistoryService {
	
	@Autowired
	private GoodsHistoryDAO goodsHistoryDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private ShopDAO shopDAO;

	@Override
	public ResultVO getGoodsAnalysis(String sessionId, String startTime, String endTime, String shopId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String systemId=sessionVO.getSystemId();
		Integer type=0;
		if(sessionVO.getIsChain()==Is.NO.toIntValue()){
			Shop shop=shopDAO.query(systemId);
			if(shop==null){
				throw new ShopNotFoundException("店铺不存在");
			}
			type=1;
		}
		if(sessionVO.getIsChain()==Is.YES.toIntValue()){
			Chain chain=chainDAO.query(systemId);
			if(chain==null){
				throw new ChainNotFoundException("品牌不存在");
			}
			if(StringUtils.isNoneBlank(shopId)){
				type=1;
				systemId=shopId;
			}	
		}
		Date sTime=DateUtil.getDateStart(DateUtil.parseDate(startTime));
		Date eTime=DateUtil.getDateEnd(DateUtil.parseDate(endTime));
		GoodsAnalysisBO gab=goodsHistoryDAO.getGoodsAnalysis(sTime, eTime, systemId,type);
		GoodsAnalysisVO gav=new GoodsAnalysisVO();
		BeanUtils.copyProperties(gab, gav);
		gav.setCartNumber(gav.getCartNumber()==null?0:gav.getCartNumber());
		gav.setExposureNumber(gav.getExposureNumber()==null?0:gav.getExposureNumber());
		gav.setGoodsRate(gav.getGoodsRate()==null?0.00:Format.decimal(gav.getGoodsRate()*100));
		gav.setGoodsViewNumber(gav.getGoodsViewNumber()==null?0:gav.getGoodsViewNumber());
		gav.setVisitUserCount(gav.getVisitUserCount()==null?0:gav.getVisitUserCount());
		result.setData(gav);
		return result;
	}

	@Override
	public Map<String, Object> getAllGoodsAnalysis(String sessionId, String startTime, String endTime,String shopId, DataTablesParamVO dtp) {
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String systemId=sessionVO.getSystemId();
		Integer type=0;
		if(sessionVO.getIsChain()==Is.NO.toIntValue()){
			Shop shop=shopDAO.query(systemId);
			if(shop==null){
				throw new ShopNotFoundException("店铺不存在");
			}
			type=1;
		}
		if(sessionVO.getIsChain()==Is.YES.toIntValue()){
			Chain chain=chainDAO.query(systemId);
			if(chain==null){
				throw new ChainNotFoundException("品牌不存在");
			}
			if(StringUtils.isNoneBlank(shopId)){
				type=1;
				systemId=shopId;
			}	
		}
		Date sTime=DateUtil.getDateStart(DateUtil.parseDate(startTime));
		Date eTime=DateUtil.getDateEnd(DateUtil.parseDate(endTime));
		List<GoodsAnalysisInfoBO> gaibList=goodsHistoryDAO.getAllGoodsAnalysis(dtp.getStart(), dtp.getLength(), sTime, eTime, systemId,type);
		List<GoodsAnalysisInfoVO> gaivList=new ArrayList<GoodsAnalysisInfoVO>();
		for(GoodsAnalysisInfoBO gaib:gaibList){
			GoodsAnalysisInfoVO gaiv=new GoodsAnalysisInfoVO();
			BeanUtils.copyProperties(gaib,gaiv);
			gaiv.setSingleRate(gaiv.getSingleRate()==null?0.00:Format.decimal(gaiv.getSingleRate()*100));
			gaiv.setExposureUserCount(gaiv.getExposureUserCount()==null?0:gaiv.getExposureUserCount());
			gaiv.setGoodsExposureNumber(gaiv.getGoodsExposureNumber()==null?0:gaiv.getGoodsExposureNumber());
			gaiv.setGoodsViewNumber(gaiv.getGoodsViewNumber()==null?0:gaiv.getGoodsViewNumber());
			gaiv.setVisitUserCount(gaiv.getVisitUserCount()==null?0:gaiv.getVisitUserCount());
			gaiv.setPayGoodsNumber(gaiv.getPayGoodsNumber()==null?0:gaiv.getPayGoodsNumber());
			gaiv.setGoodsCollections(gaiv.getGoodsCollections()==null?0:gaiv.getGoodsCollections());
			gaiv.setCartGoodsNumber(gaiv.getCartGoodsNumber()==null?0:gaiv.getCartGoodsNumber());
			gaiv.setPayUserCount(gaiv.getPayUserCount()==null?0:gaiv.getPayUserCount());
			gaiv.setOrdersGoodsNumber(gaiv.getOrdersGoodsNumber()==null?0:gaiv.getOrdersGoodsNumber());
			gaiv.setOrdersUserCount(gaiv.getOrdersUserCount()==null?0:gaiv.getOrdersUserCount());
			gaivList.add(gaiv);
		}
		Integer total=goodsHistoryDAO.getAllChainGoodsTotalCount(sTime,eTime,systemId,type);
		return ViewObjectUtil.dataTables(gaivList, dtp.getDraw(), total);
	}

}
